import numpy as np
import matplotlib.pyplot as plt

# -----  生成的散点图 ------------
x = np.linspace(0, 1, 20)
y = 3 * x + 2
noise = 0.2  # 0 ~ 1
y += np.random.normal(0, noise, (20,))
plt.plot(x, y, 'ro')
# ------ 预测线 -------------------
w = 2
b = 0.1
predict_y = w * x + b
plt.plot(x, predict_y, 'b--')
# ------ 计算损失率（计算与真实结果之间的差距）---
# ------ loss 越小越好 ----------------------
loss = np.mean((y - predict_y) ** 2)
print(f"loss:{loss:.4f}") # 保留4位小数
plt.show()
